<?php

namespace app\app\controller;

use app\BaseController;
use app\app\model\UserModel;
use app\app\model\UserRoleModel;
use app\app\model\UserPostModel;

class Login extends BaseController
{
    public function index()
    {
        $name = input('name', '');
        $password = input('password', '');
        $user = UserModel::where('user_name', $name)
            ->where('app_password', $password)
            ->field('user_id, dept_id, user_name, nick_name, user_type, email, phonenumber, sex, avatar')
            ->find();

        if (empty($user)) {
            $this->error('用户名或密码错误');
        }

        $role_list = UserRoleModel::alias('a')
            ->field('c.role_id, c.role_name, c.role_key, c.remark')
            ->where(['user_id' => $user['user_id']])
            ->join('sys_role c', 'a.role_id = c.role_id')
            ->select();

        $post_list = UserPostModel::alias('a')
            ->field('c.post_id, c.post_code, c.post_name, c.remark')
            ->where(['user_id' => $user['user_id']])
            ->join('sys_post c', 'a.post_id = c.post_id')
            ->select();

        $res = $user;
        $res['role_list'] = $role_list;
        $res['post_list'] = $post_list;

        $this->success('success', $res);
    }
}
